// stucture/calc-mvc/CalcController.java - Controller
//    Handles user interaction with listeners.
//    Calls View and Model as needed.
// Fred Swartz -- December 2004

import java.awt.event.*;

public class MCQcontroller {
    //... The Controller needs to interact with both the View.
    private MCQcview  mcq_view;
   // Tracks the current question number
    private int _questionTracker; 
   // Holds all the answers selected by user
    private int answers[];
   // Holds the current answer
    private String _answer;
    
    /** Constructor */
    MCQcontroller(MCQcview view) {
        mcq_view  = view;
	//Start at question 1
	questionTracker = 1;
	
	answers = new int[20];
        
        //... Add listeners to the view.
        mcq_view.addTestBeginListener(new TestBeginListener());
        mcq_view.addTestFinishListener(new TestFinishListener());
	mcq_view.addNextQListener(new NextQListener());
	mcq_view.addPreviousQListener(new PreviousQListener());
	mcq_view.addQSelectListener(new QSelectListener());
    }


    // return selected answer
    public String getAnswer()
    {
	return _answer;
    }
    
    
    ////////////////////////////////////////// inner class TestBeginListener

    class TestBeginListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
	
	String button = "beginTest";
	mcq_view.populateView(retriveQuestion(_questionTracker));
        }
    }//end inner class TestBeginListener
    
   ////////////////////////////////////////// inner class QSelectListener

    class QSelectListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
	
        _answer = e.getActionCommand();
	answers[questionTracker] = _answer;
	questionTracker = questionTracker + 1;
	if(questionTracker == 20)
	{
		
	}
        }
    }//end inner class QSelectListener

   ////////////////////////////////////////// inner class PreviousQListener

    class PreviousQListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
	
	questionTracker = questionTracker - 1;
        remove[answers,questionTracker];
	populateView(retriveQuestion(_questionTracker));
	questionTracker = questionTracker + 1;
        }
    }//end inner class PreviousQListener

   ////////////////////////////////////////// inner class NextQListener

    class NextQListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
	
	populateView(retriveQuestion(_questionTracker));

        }
    }//end inner class NextQListener

   ////////////////////////////////////////// inner class TestFinishListener

    class TestFinishListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
	
	String dbAnswers[] = retrieveDBAnswers();
	String results[] = compare(answers,dbAnswers);
	mcq_view.displayResults(results);

        }
    }//end inner class TestFinishListener
}

